package com.tantinale.miaoshaproject.controller;

import com.tantinale.miaoshaproject.error.BusinessException;
import com.tantinale.miaoshaproject.error.EmBusinessError;
import com.tantinale.miaoshaproject.response.CommonReturnType;
import com.tantinale.miaoshaproject.service.OrderService;
import com.tantinale.miaoshaproject.service.model.OrderModel;
import com.tantinale.miaoshaproject.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootVersion;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController("order")
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @PostMapping("/createorder")
    public CommonReturnType createOrder(@RequestParam(name="itemId") Integer itemId,
                                        @RequestParam(name="promoId") Integer promoId,
                                        @RequestParam(name="amount",required = false) Integer amount) throws BusinessException {
        //获取用户的登录信息
        Boolean isLogin = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
        if(isLogin==null||!isLogin.booleanValue()){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN,"用户未登录不能下单");
        }
        UserModel userModel =  (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");

        OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, promoId, amount);

        return CommonReturnType.create(null);
    }
}
